package com.jinzhi.jzdoor.certificate.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.type.EnumErrorCode;
import com.jinzhi.common.utils.*;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sms.config.SmsBasicConfigProperties;
import com.jinzhi.sms.support.SmsManager;
import com.jinzhi.sms.support.SmsSender;
import com.jinzhi.sys.service.UserRoleService;
import com.jinzhi.sys.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.ehcache.Element;
@Slf4j
@Controller
@RequestMapping("/enterprise")
public class EnterpriseController extends BaseController {
    @Autowired
    private MessageplatfromService messageplatfromService;
    @Autowired
    private SmsManager smsSerevice;
    @Autowired
    private SmsManager smsService;
    @Autowired
    private UserService userService;
    private UserRoleService userRoleService;

    @Log("登录")
    @PostMapping("/enLogin")
    @ResponseBody
    Result<?> enLogin(String enId) {
        if(EhCacheUtil.get(enId+"name")!=null){
            String username=EhCacheUtil.get(enId+"name").toString();
            String password=EhCacheUtil.get(enId+"pass").toString();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            Subject subject = SecurityUtils.getSubject();
            try {
                subject.login(token);
                return Result.ok();

            } catch (AuthenticationException e) {
                return Result.build(EnumErrorCode.userLoginFail.getCode(), EnumErrorCode.userLoginFail.getMsg());
            }
        }else {
            return Result.fail();
        }

    }


    @GetMapping("/enMain")
    String enMain(Model model , HttpServletRequest req) {


        UserDO userDO = userService.selectById(this.getUserId());
        model.addAttribute("name", userDO.getUsername());

        List<String> roleNames = userRoleService.selroleNameList(this.getUserId());
        model.addAttribute("roleNames", roleNames);
        req.getSession().setAttribute("roleNames" , roleNames);

        EntityWrapper entityWrapper = new EntityWrapper();
        int stuAllNum;
        int eroomAllNum;
        if(roleNames.indexOf("超级用户角色") > -1) {
//            stuAllNum = stuSignUpService.selectCount(entityWrapper);
//            eroomAllNum = examinationRoomService.selectCount(entityWrapper);

            //int orgAllNum = organizationRolesService.selectCount(entityWrapper);
            model.addAttribute("orgAllNum",0);

          //  int expAllNum = expertsService.selectCount(entityWrapper);
            model.addAttribute("expAllNum",0);

           // int stuNoReview = stuSignUpService.selNoReviewAll();
            model.addAttribute("stuNoReview",0);
           // int orgNoReview = organizationRolesService.selNoReviewAll();
            model.addAttribute("orgNoReview",0);
          //  int expNoReview = expertsService.selNoReviewAll();
            model.addAttribute("expNoReview",0);
           // int eroomNoReview = examinationRoomService.selNoReviewAll();
            model.addAttribute("eroomNoReview",0);
        }else{
          //  stuAllNum = stuSignUpService.selNoReview(this.getUser().getOrgId());
         //   eroomAllNum = examinationRoomService.selNoReview(this.getUser().getOrgId());
        }
        model.addAttribute("stuAllNum",0);
        model.addAttribute("eroomAllNum",0);

        return "main";
    }


    @GetMapping("/enterpriseSend")
    @Log("发送验证码")
    @ApiOperation("发送验证码")
    @ResponseBody
    public Result sendCode(String mobile) {
        String  prefix="enterAdd";
        Object smsDemo = EhCacheUtil.get("enterAdd");
        if(smsDemo == null){
            MessageplatfromDO messageplatfromDO = messageplatfromService.findOneByKv("注册验证码");
            if(messageplatfromDO != null){
                EhCacheUtil.put(prefix,messageplatfromDO.getSmsDemo());
                smsService.enterpriseSend(prefix, mobile,messageplatfromDO.getSmsDemo());
            }else{
                return Result.fail("短信模板错误，请联系管理员！");
            }
        }else{
            smsService.enterpriseSend(prefix, mobile,(String) smsDemo);
        }

        return Result.ok("发送成功");
    }






}
